package de.westranger.forex.v2.neural;

public class Neuron {

	public static double[] train(final double[][] input, final double[] output,	final double learningRate,final double momentum, final int epochs) {
		double[] weights = new double[input[0].length];

		for (int i = 0; i < epochs; i++) {
			for (int j = 0; j < input.length; j++) {
				double sum = 0;
				for (int k = 0; k < weights.length; k++) {
					sum += weights[k] * input[j][k];
				}
				final double diff = output[j] - tanh(sum);
				for (int k = 0; k < weights.length; k++) {
					weights[k] = weights[k] + (learningRate * diff * input[j][k]);
				}
			}
		}

		return weights;
	}
	
	public static double evaluate(final double[] weights, final double[] input) {
		double result = 0.0;
		for (int i = 0; i < weights.length; i++) {
			result += weights[i] * input[i];
		}
		return tanh(result);
	}
	
	public static double tanh(final double value) {
		return (1.0 - Math.exp(-2.0 * value)) / (1.0 + Math.exp(-2.0 * value));
	}
	
	public static double tanhDiff(final double value) {
		return (4.0 * Math.exp(2.0 * value)) / Math.pow(1.0 + Math.exp(2.0 * value), 2.0);
	}
	
}
